<?php

namespace app\common\model\wm;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'wm_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'sale_switch_text',
        'images_array',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getThumbImageAttr($value, $data)
    {
        if ($value) {
            return cdnurl($value, true);
        }
        return '';
    }
    public function getImagesArrayAttr($value, $data)
    {
        if ($data['images']) {
            $array = explode(',', $data['images']);
            foreach ($array as &$v) {
                $v = cdnurl($v, true);
            }
        }
        return [];
    }
    public function getSaleSwitchList()
    {
        return ['1' => __('Sale_switch 1'), '0' => __('Sale_switch 0')];
    }


    public function getSaleSwitchTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sale_switch']) ? $data['sale_switch'] : '');
        $list = $this->getSaleSwitchList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function category()
    {
        return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
